#include <stdio.h>
#include <stdlib.h>



int main()
{
    /*
        申请单个堆空间
    */

    //堆空间---malloc(手动申请的)
    int* p = malloc(sizeof(int));//给这个指针申请4个字节的空间
    if (p == NULL) //如果内存不够的时候 可能会出现申请失败
    {
        printf("malloc fail\n");
        return -1;
    }

    //需要手动释放
    free(p);


    char* p1 = malloc(sizeof(char) * 4);//char p1[4]
    if (p1 == NULL)
    {
        printf("malloc fail\n");
        return -1;
    }
    *p1 = 'a';
    p1[1] = 'b';
    *(p1 + 2) = 'c';
    p1[3] = 'd';
    printf("%c %c %c %c\n", p1[0], p1[1], *(p1 + 2), *(p1 + 3));
    free(p1);

    //4*4 = 16B
    int* p2 = malloc(sizeof(int) * 4);//int p2[4]
    if (p2 == NULL)
    {
        printf("malloc fail\n");
        return -1;
    }
    *p2 = 10;
    p2[1] = 20;
    *(p2 + 2) = 30;
    p2[3] = 40;
    printf("%d %d %d %d\n", p2[0], p2[1], *(p2 + 2), *(p2 + 3));
    free(p2);

    return 0;
}